package com.cloud.demo.login.service.impl;

import com.cloud.demo.common.result.BaseResult;
import com.cloud.demo.common.result.StatusCodeEnum;
import com.cloud.demo.common.result.ErrorMessage;
import com.cloud.demo.login.bean.entity.AccountInfo;
import com.cloud.demo.login.bean.dto.LoginByAccountDTO;
import com.cloud.demo.login.bean.vo.TokenVo;
import com.cloud.demo.login.service.LoginService;
import com.cloud.demo.login.util.TokenCommon;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import static com.cloud.demo.login.start.Start.ACCOUNT_MAP;

/**
 * 类说明
 *
 * @author szj
 * @date 2021/7/20
 */
@Service
@Slf4j
public class LoginServiceImpl implements LoginService {
    @Resource
    private TokenCommon tokenCommon;

    @Override
    public BaseResult<TokenVo> loginByAccount(LoginByAccountDTO loginByAccountDTO) {
        AccountInfo accountInfo = ACCOUNT_MAP.get(loginByAccountDTO.getAccountName());
        if (accountInfo == null) {
            return new BaseResult<>(StatusCodeEnum.LOGIN_FAIL_NO_ACCOUNT, ErrorMessage.ACCOUNT_MUST_BIND_PHONE, null);
        }
        if (!accountInfo.getPassword().equals(loginByAccountDTO.getPassword())) {
            return new BaseResult<>(StatusCodeEnum.LOGIN_FAIL_PASSWORD_ERROR, ErrorMessage.LOGIN_FAIL_PASSWORD_ERROR, null);
        }
        return tokenCommon.createNewToken(String.valueOf(accountInfo.getUserId()), accountInfo.getAccountName(), null, String.valueOf(accountInfo.getUserType()));
    }
}
